<?php
	
	include('../classes/clsDB.php');
	include('../classes/clsStats.php');
	include('../config.inc.php');
	$r=new ReturnRecord();
	if($_GET['sid']!=-1){
		$SQL="SELECT CCommission,affiliatePrograms.id FROM affiliateBanners,affiliatePrograms,affiliateJoinedPrograms WHERE affiliateBanners.affiliateProgramsID=affiliatePrograms.id AND affiliateBanners.id=$_GET[ab] AND affiliateJoinedPrograms.affiliateID=$_GET[aid] AND affiliatePrograms.id=affiliateJoinedPrograms.affiliateProgramsID";
		//print $SQL;
		$sq2=$r->rawQuery($SQL);  
		$myrow = mysql_fetch_row($sq2);
		if($myrow[0]>0){
			$SQL="SELECT COUNT(*) FROM affiliateClicks WHERE VisitsID='$_GET[sid]' ";
			$sq2=$r->rawQuery($SQL); 
			$data=mysql_fetch_array($sq2);
			if($data[0]==0){ 
				$SQL="INSERT INTO affiliateEarn (affiliateProgramsID,affiliateID,CommissionAmount,VisitsID,EDateTime,EType) VALUES ($myrow[1],$_GET[aid],$myrow[0],$_COOKIE[AffSid],NOW(),'Click') ";
				$sq2=$r->rawQuery($SQL);
				$SQL="UPDATE affiliate SET Account=Account+$myrow[0] WHERE id=$_GET[aid]";
				$sq2=$r->rawQuery($SQL);
			}  
		}
	}
	
	
	$SQL="SELECT TargetURL FROM affiliateBanners WHERE affiliateBanners.id=$_GET[id] ";
	//print $SQL;
	$sq2=$r->rawQuery($SQL);  
	$myrow = mysql_fetch_row($sq2);
	header("Location: http://$myrow[0]");
?>
